#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QVector>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_pReactor = new HidReactor;

    connect(m_pReactor,&HidReactor::newDevice,this,[this](QString name,HidDevice* dev){
        qDebug("add newDevice");
        if(ui->comboBox->findData(QVariant::fromValue(dev)) == -1){
            ui->comboBox->addItem(name,QVariant::fromValue(dev));
            connect(dev,&HidDevice::sigStateChanged,this,&MainWindow::DevStateCheck);
        }
    });

    connect(m_pReactor,&HidReactor::delDevice,this,[this](HidDevice* dev){
        int index = ui->comboBox->findData(QVariant::fromValue(dev));
        if(index != -1){
            ui->comboBox->removeItem(index);
        }
    });

    connect(ui->comboBox,QOverload<int>::of(&QComboBox::currentIndexChanged),this,&MainWindow::DevStateCheck);

//    select_thread = QThread::create([this](){
//        fd_set rfds;
//        while(m_bSelecting){
////            qApp->processEvents();
//            FD_ZERO(&rfds);
//            int maxfd = 0;

//            for(auto& item: m_hidDevices){
//                FD_SET(item->getHandle(),&rfds);
//                maxfd = maxfd > item->getHandle() ? maxfd : item->getHandle();
//            }
//            int ret = ::select(maxfd+1, &rfds, NULL, NULL ,NULL);
//            if(ret == -1 && errno == EINTR){
//                continue;
//            }
//            if(ret < 0){
//                perror("select");
//                return;
//            }
//            qDebug("select finish");
//            for(auto& item: m_hidDevices){
//                if(FD_ISSET(item->getHandle(),&rfds)){
//                        item->handleRead();
//                }
//            }
//        }
//    });

}
MainWindow::~MainWindow()
{
    delete ui;
    if(m_pReactor){
        m_pReactor->quit();
        m_pReactor->deleteLater();
    }
    if(m_bSelecting){
        m_bSelecting = false;
        if(select_thread){
            select_thread->wait();
            select_thread->quit();
            select_thread->deleteLater();
        }

    }
    m_hidDevices.clear();
    if(myProcess){
        myProcess->kill();
    }
}


void MainWindow::on_searchBtn_clicked()
{
    int ret = m_pReactor->findDevices();
    if(ret == 0){
        ui->comboBox->addItem("no hidDevices present",QVariant::fromValue(nullptr));
    }else{
        m_pReactor->start();
    }
}

void MainWindow::on_listenBtn_clicked()
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    //dev->UiClickProc(4);
    dev->invertOffHook();
}

void MainWindow::on_muteBtn_clicked()
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    //dev->UiClickProc(5);
    dev->invertMute();
}

void MainWindow::on_flashBtn_clicked()
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    //dev->UiClickProc(3);
    dev->invertFlash();
}

void MainWindow::on_vDecBtn_clicked()
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    dev->UiClickProc(2);
}

void MainWindow::on_vIncBtn_clicked()
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    dev->UiClickProc(1);
}

void MainWindow::DevStateCheck(int index)
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    if(index == -1){
        if(dev != qobject_cast<HidDevice*>(sender()))
            return;
    }
    auto s = dev->stateCheck();
    ui->flashBtn->setText(s.flash ? "stop flash" : "flash");
    ui->muteBtn->setText(s.mute ? "stop mute" : "mute");
    ui->listenBtn->setText(s.offhook ? "stop listen" : "listen");
}

void MainWindow::on_pushButton_clicked()
{
    QString program = "aplay";
    QStringList arguments;
    arguments << "/root/16k16bit.pcm";
    if(!myProcess){
        myProcess = new QProcess();
    }
    myProcess->start(program, arguments);
}

void MainWindow::on_releaseBtn_clicked()
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    dev->UiClickProc(3);
}

void MainWindow::on_maxVolBtn_clicked()
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    dev->UiClickProc(4);
}

void MainWindow::on_minVolBtn_clicked()
{
    auto dev = qvariant_cast<HidDevice*>(ui->comboBox->currentData());
    if(!dev)
        return;
    dev->UiClickProc(5);
}
